package com.example.fangyh09.progressbar;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    ProgressBar progressBar;
    Button add,reduce,reset;
    TextView text;
    ProgressDialog progressDialog;
    Button show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        setContentView(R.layout.activity_main);

        setProgressBarVisibility(true);
        setProgressBarIndeterminateVisibility(false);
        setProgress(9999);

        init();


    }
    void init() {
        progressBar = (ProgressBar)findViewById(R.id.horiz);
        add = (Button)findViewById(R.id.add);
        reduce = (Button)findViewById(R.id.reduce);
        reset = (Button)findViewById(R.id.reset);
        text = (TextView)findViewById(R.id.text);
        int first = progressBar.getProgress();
        int second = progressBar.getSecondaryProgress();
        int max = progressBar.getMax();
        text.setText("first is: " + (int)first * 1.0 / max * 100 + "% second is :" + (int)second * 1.0 / max * 100 + "%");
        add.setOnClickListener(this);
        reduce.setOnClickListener(this);
        reset.setOnClickListener(this);
        show = (Button)findViewById(R.id.show);
        show.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                progressBar.incrementProgressBy(10);
                progressBar.incrementSecondaryProgressBy(10);
                break;
            case R.id.reduce:
                progressBar.incrementProgressBy(-10);
                progressBar.incrementSecondaryProgressBy(-10);
                break;
            case R.id.reset:
                progressBar.setProgress(50);
                progressBar.setSecondaryProgress(80);
                break;
            case R.id.show:
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);;
                progressDialog.setTitle("mooc");
                progressDialog.setMessage("welcome");

                progressDialog.setMax(100);
                progressDialog.incrementProgressBy(50);
                progressDialog.setIndeterminate(false);

                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "sure", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,"welcom",Toast.LENGTH_SHORT).show();;
                            }
                        }
                );
                progressDialog.setCancelable(true);
                progressDialog.show();
                break;
        }

        int first = progressBar.getProgress();
        int second = progressBar.getSecondaryProgress();
        int max = progressBar.getMax();
        text.setText("first is: " + (int)first * 1.0 / max * 100 + "% second is :" + (int)second * 1.0 / max * 100 + "%");
    }
}







